// pages/crowdfunding/index/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    totalPage: 0,
    list: [],
    loadStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options);
    this.setData({
      schoolId: options.schoolId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading();
    this.setData({
      page: 1,
      totalPage: 0,
      list: [],
      loadStatus: false
    });
    this.getList(() => {
      this.setData({
        loadStatus: true
      })
      wx.hideLoading();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取活动列表
  getList(callback) {
    let that = this;
    let page = this.data.page;
    let pageSize = this.data.pageSize;
    let totalNumber = this.data.totalNumber;
    let listLength = this.data.list.length;
    if(page > 1 && listLength >= totalNumber) {
      return false;
    }

    app.request({
      url: 'crowdfunding/index',
      data: {
        page: page
      },
      sCallback: (res) => {
        if(res.code == 1) {
          page ++;
          let list = that.data.list.concat(res.data.list);
          that.setData({
            page: page,
            totalNumber: res.data.total_number,
            list: list
          });
        }
        callback && callback();
      }
    })
  },
  //编辑
  edit(e){
    let index = app.getBindParam(e, 'index');
    let detail = this.data.list[index];
    let  url="/pages/crowdfunding/edit/index?data="+encodeURIComponent(JSON.stringify(detail));
    wx.navigateTo({
      url: url
    })
  },
  //去机构详情
  toCourse(e) {
    let courseId = app.getBindParam(e, 'id');
    wx.navigateTo({
      url: '/pages/classdetail/index?courseId='+courseId
    })
  }
})